<template>
  <div class="student-info-header" v-bind="$attrs">
    <!-- 修改的照片容器部分 -->
    <div class="photo-container">
      <div v-if="imageLoading" class="image-loading">
        <div class="loading-spinner"></div>
      </div>
      <img class="student-photo" :src="student.photo" alt="StudentPhoto" @load="handleImageLoad"
        @error="handleImageError" />
    </div>

    <!-- 完全保持原有的学生详情部分 -->
    <div class="student-details">
      <h2 class="image-text">{{ student.name }}</h2>
      <p class="student-gender">{{ student.gender }}</p>
      <p v-if="student.scholarship_grants && student.scholarship_grants.trim() !== ''" class="student-gender"
        style="color: red;">
        {{ student.scholarship_grants }}
      </p>

      <div v-if="student.politicalStatus && student.politicalStatus.trim() !== ''" class="politicalStatus-div">
        <img :src="student.politicalStatus === '中共党员' ? '/profilePage/OIP.png' : '/profilePage/small1.png'"
          class="Himg" />
        <div class="image-text-container">
          <p>{{ student.politicalStatus }}</p>
        </div>
      </div>
    </div>
  </div>

  <!-- 完全保持原有的信息体部分 -->
  <div class="student-info-body">
    <div class="info-item">
      <span class="label">学号:</span>
      <span class="value">{{ student.id }}</span>
    </div>
    <div class="info-item">
      <span class="label">出生年月:</span>
      <span class="value">{{ student.dob }}</span>
    </div>
    <div class="info-item">
      <span class="label">生源地:</span>
      <span class="value">{{ student.position }}</span>
    </div>
    <div class="info-item">
      <span class="label">招生形式:</span>
      <span class="value">{{ student.admissionType }}</span>
    </div>
    <div class="info-item">
      <span class="label">专业:</span>
      <span class="value">{{ student.major }}</span>
    </div>
    <div class="info-item">
      <span class="label">班级:</span>
      <span class="value">{{ student.class }}</span>
    </div>
    <div class="info-item">
      <span class="label">宿舍号:</span>
      <span class="value">{{ student.dormNumber }}</span>
    </div>
    <div class="info-item">
      <span class="label">学业导师:</span>
      <span class="value">{{ student.academicAdvisor }}</span>
    </div>
  </div>
</template>

<script setup>
import { ref, watch, reactive } from 'vue'
import { get_data } from '../../../../api/api';

// 完全保持原有的响应式对象
const student = reactive({
  id: '',
  name: '暂无数据',
  photo: '/profilePage/download.jpg',
  gender: '',
  dob: '',
  politicalStatus: '',
  position: '',
  scholarship_grants: '',
  major: '',
  class: '',
  dormNumber: '',
  academicAdvisor: '',
  admissionType: ''
});

// 新增的照片加载状态逻辑
const imageLoading = ref(true);

const handleImageLoad = () => {
  imageLoading.value = false;
};

const handleImageError = (e) => {
  imageLoading.value = true;
  const fallbackSrc = '/profilePage/student1.jpg';
  const img = new Image();
  img.src = fallbackSrc;
  img.onload = () => {
    e.target.src = fallbackSrc;
    imageLoading.value = false;
  };
  img.onerror = () => {
    imageLoading.value = false;
  };
};

// 监听照片URL变化
watch(() => student.photo, (newVal) => {
  imageLoading.value = true;
  const img = new Image();
  img.src = newVal;
  img.onload = () => handleImageLoad();
  img.onerror = () => handleImageError({ target: { src: newVal } });
});

// 完全保持原有的props定义
const props = defineProps({
  selectedStudentId: {
    type: Number,
    required: false,
    default: 1,
  },
});

// 完全保持原有的数据获取逻辑
const getToken = () => {
  return localStorage.getItem('pz_token');
};

const fetchData = async (StudentID) => {
  try {
    const token = getToken();
    if (!token || !StudentID) return;

    const headers = { 'Authorization': `JWT ${token}` };
    const response = await get_data(StudentID, headers);
    const studentData = response.data.data.data;

    // 完全保持原有的数据赋值方式
    student.id = studentData.StudentID;
    student.name = studentData.Name;
    student.photo = studentData.Avatar || '/profilePage/student1.jpg';
    student.gender = studentData.Gender;
    student.dob = studentData.EnrollmentDate;
    student.politicalStatus = studentData.PoliticalStatus;
    student.position = studentData.Hometown;
    student.scholarship_grants = studentData.scholarship_grants;
    student.major = studentData.Major;
    student.class = studentData.ClassName;
    student.dormNumber = studentData.DormitoryNumber;
    student.academicAdvisor = studentData.AcademicAdvisor;
    student.admissionType = studentData.AdmissionType;
  } catch (error) {
    console.error('获取数据失败:', error);
  }
};

// 完全保持原有的watch监听
watch(() => props.selectedStudentId, (newValue) => {
  if (newValue) fetchData(newValue);
});
</script>

<style scoped>
/* 基础样式 */
.student-info-header {
  display: flex;
  padding: 10px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
  margin-bottom: 15px;
}

/* 照片容器样式 */
.photo-container {
  position: relative;
  width: 100px;
  height: 130px;
  margin-right: 20px;
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.2);
}

.student-photo {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 8px;
}

/* 学生详情样式 */
.student-details {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.student-details h2 {
  font-size: 24px;
  color: #ffffff;
  margin: 0 0 8px 0;
  font-weight: 500;
}

.student-gender {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.8);
  margin: 4px 0;
}

.politicalStatus-div {
  display: flex;
  align-items: center;
  margin-top: 8px;
}

.Himg {
  width: 24px;
  height: 24px;
  margin-right: 8px;
}

.image-text-container {
  background: rgba(255, 255, 255, 0.1);
  padding: 4px 12px;
  border-radius: 4px;
}

.image-text-container p {
  margin: 0;
  color: #ffffff;
  font-size: 14px;
}

/* 信息主体样式 */
.student-info-body {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px;
  padding: 15px;
  background: rgba(255, 255, 255, 0.05);
  border-radius: 8px;
}

.info-item {
  display: flex;
  align-items: center;
}

.label {
  color: rgba(255, 255, 255, 0.6);
  font-size: 14px;
  width: 80px;
  flex-shrink: 0;
}

.value {
  color: #ffffff;
  font-size: 14px;
}

/* 加载动画样式 */
.image-loading {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
}

.loading-spinner {
  width: 24px;
  height: 24px;
  border: 2px solid rgba(255, 255, 255, 0.3);
  border-top: 2px solid #ffffff;
  border-radius: 50%;
  animation: spin 1s linear infinite;
}

@keyframes spin {
  0% { transform: rotate(0deg); }
  100% { transform: rotate(360deg); }
}
</style>